package shop.cart.service.impl;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import shop.cart.mapper.CartMapper;
import shop.cart.remote.RemoteProductService;
import shop.cart.service.CartService;
import shop.common.core.Result;
import shop.common.entity.Cart;
import shop.common.entity.Product;
import shop.common.utils.ConvertUtil;

import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements CartService {

    private final RemoteProductService productService;

    @Override
    public Cart details(String id) {
        Cart cart = baseMapper.selectById(id);
        Result result = productService.details(cart.getProductId());
        if (result.getCode().equals(200)) {
            Product product = ConvertUtil.cast(result.getData(), Product.class);
            cart.setProduct(product);
        }
        return cart;
    }


    @Override
    public boolean removeByUserIdAndProductId(String userId, String productId) {
        int delete = baseMapper.delete(Wrappers.lambdaQuery(Cart.class).eq(Cart::getUserId, userId).eq(Cart::getProductId, productId));
        return delete > 0;
    }

    @Override
    public List<Cart> listByUserId(String userId) {
        List<Cart> cartList = baseMapper.selectList(Wrappers.lambdaQuery(Cart.class)
                .eq(Cart::getUserId, userId));

        return cartList.stream().map(cart -> this.details(cart.getId())).collect(Collectors.toList());
    }

    @Override
    public boolean increase(String userId, String productId) {
        Cart one = baseMapper.selectOne(Wrappers.lambdaQuery(Cart.class)
                .eq(Cart::getProductId, productId)
                .eq(Cart::getUserId, userId));
        int res = 0;
        if (one == null) {
            one = new Cart();
            one.setUserId(userId);
            one.setProductId(productId);
            one.setQuantity(1);
            res = baseMapper.insert(one);
        } else {
            one.setQuantity(one.getQuantity() + 1);
            res = baseMapper.updateById(one);
        }

        return res > 0;
    }

    @Override
    public boolean decrease(String userId, String productId) {
        Cart one = baseMapper.selectOne(Wrappers.lambdaQuery(Cart.class)
                .eq(Cart::getProductId, productId)
                .eq(Cart::getUserId, userId));
        int res = 0;
        if (one.getQuantity() == 1) {
            res = baseMapper.deleteById(one.getId());
        } else {
            one.setQuantity(one.getQuantity() - 1);
            res = baseMapper.updateById(one);
        }

        return res > 0;
    }

    @Override
    public boolean clearByUserId(String userId) {
        int delete = baseMapper.delete(Wrappers.lambdaQuery(Cart.class)
                .eq(Cart::getUserId, userId));
        return delete > 0;
    }

    @Override
    public void select(String id) {
        Cart one = baseMapper.selectById(id);
        one.setSelected(one.getSelected() == 0 ? 1 : 0);
        baseMapper.updateById(one);
    }
}




